package com.example.tcp;


import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;

public class RedissonTest {
    public static void main(String[] args) {
        Config config=new Config();
        config.useSingleServer().setAddress("redis://120.77.168.198:6379").setPassword("ysh123").setDatabase(0);  //进行连接redis
        StringCodec stringCodec=new StringCodec();
        config.setCodec(stringCodec);
        RedissonClient redissonClient = Redisson.create(config);


        //对字符串的操作   基本的  key  value
        RBucket<Object> im = redissonClient.getBucket("im");
        System.out.println(im.get());
        im.set("hello redisson");
        System.out.println(im.get());

        //对map的操作
        RMap<Object, Object> immap = redissonClient.getMap("immap");
        System.out.println(immap.get("clint"));
        immap.put("clint","hahaha");
        System.out.println(immap.get("clint"));

        //redis使用发布订阅
        RTopic topic = redissonClient.getTopic("topic");    // 用户1 进行监听
        topic.addListener(String.class, new MessageListener<String>() {
            @Override
            public void onMessage(CharSequence charSequence, String s) {
                System.out.println("topic get message "+ s);
            }
        });
        RTopic topic2 = redissonClient.getTopic("topic");   //用户2 进行监听
        topic2.addListener(String.class, new MessageListener<String>() {
            @Override
            public void onMessage(CharSequence charSequence, String s) {
                System.out.println("topic2 get message "+ s);
            }
        });

        RTopic topic3 = redissonClient.getTopic("topic");
        topic3.publish("topic3 send hello ysh will study redisson");


    }
}
